<template>
  <!-- ref可以帮助获取dom -->
  <div class="list" ref="wrapper">
    <div>
        <div class="area">
            <div class="title border-topbottom">当前城市</div>
            <div class="button-list">
                <div class="button-wrapper">
                    <div class="button">{{this.currenCity}}</div>
                </div>
            </div>
        </div>
        <div class="area">
            <div class="title border-topbottom">热门城市</div>
            <div class="button-list">
                <div
                  class="button-wrapper"
                  v-for="item of hot"
                  :key="item.id"
                  @click="handleCityClick(item.name)"
                >
                    <div class="button">{{item.name}}</div>
                </div>
            </div>
        </div>
        <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
            <div class="title border-topbottom">{{key}}</div>
            <div class="item-list">
              <div
                class="item border-bottom"
                v-for="innerItem of item"
                :key="innerItem.id"
                @click="handleCityClick(innerItem.name)"
              >
                {{innerItem.name}}
              </div>
            </div>
        </div>
    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currenCity: 'city'
    })
  },
  methods: {
    handleCityClick: function (city) {
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  watch: { // 监听letter值的变化，一旦letter改变就做点事
    letter () {
      if (this.letter) {
        // 这个方法可以让滚动区滚动到某个元素的位置
        // this.$refs[this.letter]会返回一个数组，所以后面加[0]
        const element = this.$refs[this.letter][0]
        // scrollToElement函数必须接收一个dom元素
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    // this.scroll = new BScroll(this.$refs.wrapper)
    this.$nextTick(() => {
      let bscrollDom = this.$refs.wrapper
      this.scroll = new BScroll(bscrollDom, {mouseWheel: true, click: true, tap: true})
    })
  },
  updated () {
    this.scroll.refresh()
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    // 此时页面无法滚动了，借助别的插件
    overflow: hidden
    position: absolute
    top: 77px
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: 30px
      background: #eee
      padding-left: 12px
      color: #666
      font-size: 14px
    .button-list
      overflow: hidden
      padding: 5px 30px 5px 5px
      .button-wrapper
        float: left
        width: 33.33%
        .button
          margin: 5px
          padding: 5px 0
          text-align: center
          border: 1px solid #ccc
          border-radius: 3px
    .item-list
      .item
        line-height: 34px
        padding-left: 12px
        font-size: 14px
</style>
